package cn.unknownerror.ueteam.gui.frames;

import cn.unknownerror.ueteam.data.ConfigurationManager;
import cn.unknownerror.ueteam.gui.AbstractFrame;
import cn.unknownerror.ueteam.gui.DynamicButton;
import cn.unknownerror.ueteam.gui.FrameManager;
import cn.unknownerror.ueteam.team.ITeam;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;

public class PlayerButton extends DynamicButton<ITeam> {

    private OfflinePlayer player;
    private ConfigurationManager config;

    public PlayerButton(OfflinePlayer player,ConfigurationManager config){
        super(null);
        this.player=player;
        this.config=config;
    }

    @Override
    public ItemStack onCreate(ITeam team) {
        String str = "GUI.Customize.member.";
        ItemStack item = new ItemStack(Material.getMaterial(str + "material"));
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(replace(config.getString(str + "display_name"), team));
        List<String> lore = new ArrayList<>();
        for (String str1 : config.getStringList(str + "lore")) {
            lore.add(replace(str1, team));
        }
        item.setItemMeta(meta);
        return item;
    }

    private String replace(String str,ITeam team){
        return str.replaceAll("%member_name%",player.getName())
                .replaceAll("%member_state%",String.valueOf(player.isOnline()));
    }

    @Override
    public void onClick(Player player){
        for(AbstractFrame frame:FrameManager.getManager().getFrames()){
            if(frame instanceof PlayerManageFrame){
                ((PlayerManageFrame)frame).update(player);
                player.closeInventory();
                player.openInventory(((PlayerManageFrame) frame).createInventory(null));
                return;
            }
        }
    }
}
